package com.动态代理;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxy {


    public static void main(String[] args) {



        Boy boy = new Boy();


        /**
         * 参数1 ： 类加载器，
         * 参数2 ： 要代理的interface
         * 参数3：  代理处理类
         */
        Hello hello = (Hello) Proxy.newProxyInstance(
                ClassLoader.getSystemClassLoader(),
                new Class[]{Hello.class},
                new DynamicProxy(boy)
        );


        hello.say("aaa");









    }





}


class DynamicProxy implements InvocationHandler {

    private Hello hello;

    public DynamicProxy(Hello hello) {
        this.hello = hello;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(Thread.currentThread().getName());
        return method.invoke(hello,args);
    }
}

class People implements Serializable {


    public People(Integer id, String name, boolean sex) {
        this.id = id;
        this.name = name;
        this.sex = sex;
    }

    private static final long serialVersionUID = -4105220406578295629L;
    private Integer id;

    private String name ;

    private boolean sex;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }
}


class Boy  implements Hello{

    public String say(String x){
        return x;
    }
}

interface  Hello{
    String say(String x);
}

